package ophiux.api.sdk.client;

import java.util.LinkedHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ophiux.api.sdk.OphiuxApiException;
import ophiux.api.sdk.constant.OphiuxApiSDKConstant;
import ophiux.api.sdk.request.OphiuxBaseRequest;
import ophiux.api.sdk.request.WsRequestHeader;
import ophiux.api.sdk.response.OphiuxResponse;
import ophiux.common.net.INetApiRequest;
import ophiux.common.net.WebserviceApiRequest;
import ophiux.common.parse.IOphiuxApiParser;
import ophiux.common.parse.json.OphiuxApiJsonParser;
import ophiux.common.parse.xml.OphiuxApiXmlParser;
import ophiux.common.proxy.NetApiRequestProxy;
import ophiux.common.utils.webserviceUtil;

/**
 * 调用   传参通过xml字符串
 * 说明：<br>
 * <h1>创 建 人: hehailong   <br></h1>
 * 创建日期: 2017-11-17 上午11:44:36<br>
 * 需要的jar包:
 */
@Deprecated
public class DefaultOphiuxClient implements IOphiuxClient{
	private static Logger logger = LoggerFactory.getLogger(DefaultOphiuxClient.class);
 
	private String userName;
	private String userPassword;
	private String token;
	private String format = OphiuxApiSDKConstant.format_xml;	
	private String serviceURL = "";
	
	public DefaultOphiuxClient(){
		
	}
	
	public DefaultOphiuxClient(String _serviceURL,String _userName,String _userPassword,
			String _token){
		this.serviceURL = _serviceURL;
		this.userName = _userName;
		this.userPassword = _userPassword;
		this.token = _token;
	}
	
	public DefaultOphiuxClient(String _serviceURL,String _userName,String _userPassword,
			String _token,String _format){
		this(_serviceURL,_userName,_userPassword,_token);
		this.format = _format;
	}
	
	
	@Override
	public <T extends OphiuxResponse> T executor(OphiuxBaseRequest<T> _request) 
											throws OphiuxApiException {
		IOphiuxApiParser<T> parser = null;
		Class clzz = _request.getResponseClass();
		if(format.equals(OphiuxApiSDKConstant.format_xml)){
			parser = new OphiuxApiXmlParser<T>(clzz);
		}else if(format.equals(OphiuxApiSDKConstant.format_json)){
			parser = new OphiuxApiJsonParser<T>(clzz);
		}
		T responseEntity = doSoapApiRequest(_request, parser);
		responseEntity.setRequestParam(_request.getTextParam());
		return responseEntity;
	}
	
	public <T extends OphiuxResponse> T doSoapApiRequest
												(OphiuxBaseRequest<T> _request,IOphiuxApiParser<T> parser) 
											    throws OphiuxApiException{
		String httpSoapParam = getHttpSoapParam(_request);
		INetApiRequest netApiRequest = new WebserviceApiRequest();
		NetApiRequestProxy netApiReqProxy = new NetApiRequestProxy(netApiRequest);
		
		
		
//		System.out.println("请求参数:"+httpSoapParam);
		logger.info("标准服务请求参数:"+httpSoapParam);
		String response = netApiReqProxy.request(serviceURL,"POST", httpSoapParam);
//		System.out.println("标准服务返回参数:"+response);
		logger.info("标准服务返回参数:"+response);
		T responseEntity = parser.parser(response);
		responseEntity.setRealResponseDataSource(response);
		return responseEntity;
	}
	
	public String getHttpSoapParam(OphiuxBaseRequest _request){
		WsRequestHeader requestHead = new WsRequestHeader();
		requestHead.setR_FORM(format);
		requestHead.setTOKEN(token);
		requestHead.setUSER_NAME(userName);
		requestHead.setUSER_PWD(userPassword);
		String bParamXML = _request.getTextParam();
		logger.info("业务参数:"+bParamXML);
		String requestParamXML = _request.toRequestParam(requestHead, bParamXML);
		requestParamXML = "<![CDATA["+requestParamXML+"]]>";
		LinkedHashMap soapParamMap = new LinkedHashMap<String,Object>();
		soapParamMap.put("arg0", requestParamXML);
		soapParamMap.put("arg1", format);
		String soapParam = webserviceUtil.getHttpSoapParam(_request.getApiMethod(), 
									soapParamMap, OphiuxApiSDKConstant.SOAP_NAMESPACE);
		
		return soapParam;
	}
	
	
}
